﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KeMei.Common;


public partial class Manager_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ViewState["GUID"] = System.Guid.NewGuid().ToString();
            this.lblGUID.Text = this.ViewState["GUID"].ToString();


            KeMei.BLL.SysParameters bll = new KeMei.BLL.SysParameters();
            KeMei.Model.SysParameters info1 = bll.GetModel(2);
            if (info1 != null)
            {
                lbCopyright.Text = info1.TValue;
            }
        }
    }

    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnLogin_Click(object sender, ImageClickEventArgs e)
    {
        #region 验证登录次数
        if ((Session["PassErrorCountAdmin"] != null) && (Session["PassErrorCountAdmin"].ToString() != ""))
        {
            int PassErroeCount = Convert.ToInt32(Session["PassErrorCountAdmin"]);
            if (PassErroeCount > 3)
            {
                txtUsername.Disabled = true;
                txtPass.Disabled = true;
                btnLogin.Enabled = false;
                this.lblMsg.Text = "对不起，你错误登录了三次，系统登录锁定！";
                return;
            }

        }
        #endregion

        #region 检查验证码
        if ((Session["CheckCode"] != null) && (Session["CheckCode"].ToString() != ""))
        {
            if (Session["CheckCode"].ToString().ToLower() != this.CheckCode.Value.ToLower())
            {
                this.lblMsg.Text = "所填写的验证码与所给的不符 !";
                Session["CheckCode"] = null;
                return;
            }
            else
            {
                Session["CheckCode"] = null;
            }
        }
        else
        {
            Response.Redirect("Login.aspx");
        }
        #endregion

        string userName = PageValidate.InputText(txtUsername.Value.Trim(), 30);
        string Password = PageValidate.InputText(txtPass.Value.Trim(), 30);

        KeMei.BLL.SysUser bll = new KeMei.BLL.SysUser();
        KeMei.BLL.SysUserWithRoles rbll=new KeMei.BLL.SysUserWithRoles();
        try
        {
            KeMei.Model.SysUser model = null;
            model = bll.ValidateUser(userName,DESEncrypt.Encrypt(Password));
            if (model == null)
            {
                this.lblMsg.Text = "登陆失败： " + userName;
                if ((Session["PassErrorCountAdmin"] != null) && (Session["PassErrorCountAdmin"].ToString() != ""))
                {
                    int PassErroeCount = Convert.ToInt32(Session["PassErrorCountAdmin"]);
                    Session["PassErrorCountAdmin"] = PassErroeCount + 1;
                }
                else
                {
                    Session["PassErrorCountAdmin"] = 1;
                }
            }
            else
            {
                Session["UserInfo"] = model;
                Session["RoleID"] = model.RoleID;
                Session["UserID"] = model.ID;
                Response.Redirect("main.aspx", true);
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show(this, ex.Message);
        }
        finally
        { 
           
        }
    }
}